/*!
 * Page: mall/publish/buyRules/BuyRules
 * License: MIT
 * Created: 2019-11-23 15:17
 */

/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */

Page({
  data: {
    buyRules: {} as any,
  },
  onLoad() {
    this.getLastPageData()
  },
  /** 获取上一页的数据 */
  getLastPageData() {
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    const buyRules = lastpage.data.buyRules
    this.setData({
      buyRules,
    })
  },
  /** 输入起订数量 */
  inputStartNumber(e: WechatMiniprogram.CustomEvent) {
    const start = e.detail.value
    const buyRules = this.data.buyRules
    buyRules.start = start
    this.setData({
      buyRules,
    })
  },
  /** 输入购买周期 */
  inputweek(e: WechatMiniprogram.CustomEvent) {
    const week = e.detail.value
    const buyRules = this.data.buyRules
    buyRules.week = week
    this.setData({
      buyRules,
    })
  },
  /** 输入购买数量 */
  inputNumber(e: WechatMiniprogram.CustomEvent) {
    const number = e.detail.value
    const buyRules = this.data.buyRules
    buyRules.number = number
    this.setData({
      buyRules,
    })
  },
  submit() {
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    const buyRules = this.data.buyRules
    if (buyRules.week > 0 && buyRules.number < 1) {
      wx.showToast({
        title: '请输入商品数量限制',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    lastpage.setData({
      buyRules,
    })
    wx.navigateBack({ delta: 1 })
  },
})
